package ui;

/**
 * EDC
 * 
 * This is the main entry point of applet mode
 * The interface allows to invoke various operations to analyze DTDs by files or folders
 * See README file for more information and running
 */

import java.awt.*;
import java.applet.*;
import java.beans.PropertyVetoException;

import javax.swing.*;

import utils.EDCResource;
import utils.EDCUtils;
import views.EDCAddDTDViewer;
import views.EDCAnalyzeViewer;
import views.EDCHomeViewer;
import views.EDCManagerViewer;
import views.EDCNavigator;
import views.EDCParseViewer;
import views.EDCTextViewer;
import views.EDCTreeViewer;


public class EDC extends JApplet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public static final String message = "Welcome!";

	public static EDC current;

	private JDesktopPane desktopLeft;
	private JDesktopPane desktopRight;

	private Font font = new Font("serif", Font.ITALIC + Font.BOLD, 36);

	private EDCToolBarPanel toolbar;
	private EDCStatusPanel statusPane;
	private EDCNavigator navigator;
	private EDCHomeViewer homeViewer;
	
	private EDCTextViewer textViewer;
	private EDCTreeViewer treeViewer;
	private EDCParseViewer parseViewer;
	private EDCAddDTDViewer addDTDViewer;
	private EDCAnalyzeViewer analyzeViewer;
	private EDCManagerViewer preferenceViewer;
	
	private JSplitPane splitPane;
	
	private String filename;
	
	private Color bg = new Color(220, 220, 220);

	public void init() {

		// set the default look and feel
		String laf = UIManager.getSystemLookAndFeelClassName();
		try {
			UIManager.setLookAndFeel(laf);
		} catch (UnsupportedLookAndFeelException exc) {
			System.err.println ("Warning: UnsupportedLookAndFeel: " + laf);
		} catch (Exception exc) {
			System.err.println ("Error loading " + laf + ": " + exc);
		}
		getContentPane().setLayout (new BorderLayout());

		toolbar = new EDCToolBarPanel();
		getContentPane().add(toolbar, BorderLayout.NORTH);

		current = this;
		
		desktopLeft = new JDesktopPane();
		desktopLeft.setBackground(bg);
		
		navigator = new EDCNavigator();
		navigator.setSize(new Dimension(170,550));
		navigator.setLocation(0, 0);
		desktopLeft.add(navigator, JLayeredPane.DEFAULT_LAYER);
		getContentPane().add(desktopLeft, BorderLayout.WEST);

		desktopRight = new JDesktopPane();
		desktopRight.setBackground(bg);
		
		preferenceViewer = new EDCManagerViewer();
		desktopRight.add(preferenceViewer, JLayeredPane.POPUP_LAYER);

		//Create a split pane with the two scroll panes in it.
		splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
				desktopLeft, desktopRight);
//		splitPane.setOneTouchExpandable(true);
		splitPane.setContinuousLayout(true);
		splitPane.setDividerLocation(150);
		splitPane.setResizeWeight(0.5);

		//Provide a preferred size for the split pane.
		splitPane.setPreferredSize(new Dimension(800, 600));
		getContentPane().add(splitPane, BorderLayout.CENTER);
		
		//Status panel
		statusPane = new EDCStatusPanel();
		getContentPane().add(statusPane, BorderLayout.SOUTH);
		
		repaint();
		System.out.println(System.getProperty("user.dir"));
	}

	public void paint (Graphics g) {
		super.paint(g);
		g.setColor(Color.blue);
		g.setFont(font);
//		g.drawString(message, 40, 80);
		
	}
	
	public void repaint() {
		super.repaint();
		if(EDCResource.TEXT_VIEW_REQUIRED) {
			textViewer = new EDCTextViewer(filename);
			if(!textViewer.isShowing()){
				desktopRight.add(textViewer, JLayeredPane.POPUP_LAYER);
				desktopRight.repaint();
			}	
			return;
		}
		if(EDCResource.DTD_VIEW_REQUIRED) {
			treeViewer = new EDCTreeViewer(filename);
			desktopRight.add(treeViewer, JLayeredPane.POPUP_LAYER);
			desktopRight.repaint();
			return;
		}
		if(EDCResource.ANALYZE_FILE_REQUIRED) {
			parseViewer = new EDCParseViewer(filename);
			desktopRight.add(parseViewer, JLayeredPane.POPUP_LAYER);
			desktopRight.repaint();
			return;
		}
		if(EDCResource.ANALYZE_FOLDER_REQUIRED) {
			analyzeViewer = new EDCAnalyzeViewer();
			desktopRight.add(analyzeViewer, JLayeredPane.POPUP_LAYER);
			desktopRight.repaint();
			return;
		}
		if(EDCResource.MANAGER_VIEW_REQUIRED) {
			preferenceViewer = new EDCManagerViewer();
			desktopRight.add(preferenceViewer, JLayeredPane.POPUP_LAYER);
			desktopRight.repaint();
			return;
		}
		if(EDCResource.DTD_INFOS_REQUIRED) {
			homeViewer = new EDCHomeViewer();
			desktopRight.add(homeViewer, JLayeredPane.DEFAULT_LAYER);
			desktopRight.repaint();
			return;
		}
		if(EDCResource.ADD_DTD_REQUIRED) {
			addDTDViewer = new EDCAddDTDViewer();
			desktopRight.add(addDTDViewer, JLayeredPane.POPUP_LAYER);
			desktopRight.repaint();
			return;
		}
//		else {
//			if(addDTDViewer!=null) {
//				try {
//					addDTDViewer.setClosed(true);
//				} catch (PropertyVetoException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//			}
//		}
	}
	

	public void refreshNavigator() {
		//refresh navigator tree
		navigator.resetTree();
		navigator.createNodes(EDCNavigator.getCurrent().getRoot(), EDCResource.inputFile.getName());
		navigator.getTree().updateUI();
	}
	
	public void refreshHomeViewer() {
		//refresh home viewer
		homeViewer.getEDCTableModel().getData().removeAllElements();
		homeViewer.getEDCTableModel().setData(EDCResource.URL_CATALOG_PATH);
	}
	
	public static EDC getCurrent(){
		return current;
	}

	/**
	 * @return the homeViewer
	 */
	public EDCHomeViewer getHomeViewer() {
		return homeViewer;
	}

	/**
	 * @param homeViewer the homeViewer to set
	 */
	public void setHomeViewer(EDCHomeViewer homeViewer) {
		this.homeViewer = homeViewer;
	}

	/**
	 * @return the textViewer
	 */
	public EDCTextViewer getTextViewer() {
		return textViewer;
	}

	/**
	 * @param textViewer the textViewer to set
	 */
	public void setTextViewer(EDCTextViewer textViewer) {
		this.textViewer = textViewer;
	}

	/**
	 * @return the treeViewer
	 */
	public EDCTreeViewer getTreeViewer() {
		return treeViewer;
	}

	/**
	 * @param treeViewer the treeViewer to set
	 */
	public void setTreeViewer(EDCTreeViewer treeViewer) {
		this.treeViewer = treeViewer;
	}

	/**
	 * @return the parseViewer
	 */
	public EDCParseViewer getParseViewer() {
		return parseViewer;
	}

	/**
	 * @param parseViewer the parseViewer to set
	 */
	public void setParseViewer(EDCParseViewer parseViewer) {
		this.parseViewer = parseViewer;
	}

	/**
	 * @return the addDTDViewer
	 */
	public EDCAddDTDViewer getAddDTDViewer() {
		return addDTDViewer;
	}

	/**
	 * @param addDTDViewer the addDTDViewer to set
	 */
	public void setAddDTDViewer(EDCAddDTDViewer addDTDViewer) {
		this.addDTDViewer = addDTDViewer;
	}

	/**
	 * @return the filename
	 */
	public String getFilename() {
		return filename;
	}

	/**
	 * @param filename the filename to set
	 */
	public void setFilename(String filename) {
		this.filename = filename;
	}

	/**
	 * @return the analyzeViewer
	 */
	public EDCAnalyzeViewer getAnalyzeViewer() {
		return analyzeViewer;
	}

	/**
	 * @param analyzeViewer the analyzeViewer to set
	 */
	public void setAnalyzeViewer(EDCAnalyzeViewer analyzeViewer) {
		this.analyzeViewer = analyzeViewer;
	}

	/**
	 * @return the statusPane
	 */
	public EDCStatusPanel getStatusPane() {
		return statusPane;
	}

	/**
	 * @param statusPane the statusPane to set
	 */
	public void setStatusPane(EDCStatusPanel statusPane) {
		this.statusPane = statusPane;
	}

	/**
	 * @return the navigator
	 */
	public EDCNavigator getNavigator() {
		return navigator;
	}

	/**
	 * @param navigator the navigator to set
	 */
	public void setNavigator(EDCNavigator navigator) {
		this.navigator = navigator;
	}
	
	

}
